//请求的服务端路径均相对于此路径
//var baseUrl = GetBasePath(location.href);
var baseUrl = GetApiRootUrl();
var baseUrlMall = GetApiRootUrlMall();

//获取根路径，自动解析本应用程序路径和安装后根路径问题
function GetBasePath(path) {
    var regex = /\w+(\/pluginview\/(\w+)\/)\w+/;
    if (regex.test(path)) {
        return RegExp.$1;
    }
    return "/";
}

function GetApiRootUrl() {
    var regex = /\w+(\/PluginView\/(\w+)\/)\w+/gi;
    if (regex.test(location.href)) {
        return "/" + RegExp.$2 + "/";
    }
    return "/Qt/";
}

function GetApiRootUrlMall() {
    var regex = /\w+(\/PluginView\/(\w+)\/)\w+/gi;
    if (regex.test(location.href)) {
        return "/" + RegExp.$2 + "/";
    }
    return "/Qt/";
}

//信息提示框
function tost(msg) {
    $(".tost").html(msg);
    $(".tost").stop(true, false).fadeIn(1000).fadeOut(2000);
}
//遮罩层
function showLoad() {
    if ($('#loading').length == 0) {
        $('<div id="loading"><img style="display:block;margin:60% 42%;" src="phoneContent/img/loadding.gif" /></div>').appendTo('body');
    } else {
        $('#loading').css('display', 'block');
    }
}

function hideLoad() {
    $('#loading').remove();
}
//获取参数
function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
}
var logined = true;
//登录过期返回登录页
function returnLogin(r) {
    if (r.ErrorCode == -2) {
    	setTimeout(function(){
    		if(r.RedirectUrl){
    			location.href = r.RedirectUrl;
    		}else{
    			location.href = 'login.html';
    		}
    	},500)
    }
    if(r.ErrorCode == -1){
    	if(r.ErrorMsg=='站点已关闭！'){
    		if(r.RedirectUrl){
    			location.href = r.RedirectUrl;
    		}else{
    			location.href = 'login.html';
    		}
    	}
    }
}

function convertCanvasToImage(canvas) {  
    //新Image对象，可以理解为DOM  
    var image = new Image();  
    // canvas.toDataURL 返回的是一串Base64编码的URL，当然,浏览器自己肯定支持  
    // 指定格式 PNG  
    image.src = canvas.toDataURL("image/png");  
    return image;  
}
//例
//  var mycanvas1=document.getElementsByTagName('canvas')[0];
//	var img=convertCanvasToImage(mycanvas1);
//	$('#qrcode').append(img);


//中文编码格式转换
function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
} 




//ajax
var common = function(window) {
    var qb = {};
    //提示框
    qb.layer = function(layerTitle, layerType, layerArea, layerContent, layerSuccess) {
            var layerArea = layerArea || ['330px', '180px'];
            layer.open({
                title: layerTitle,
                type: 1,
                area: layerArea,
                shadeClose: true, //点击遮罩关闭
                btn: ['确定', '取消'],
                content: layerContent,
                yes: function(index, layero) {
                    if (layerSuccess) {
                        layerSuccess();
                    }
                    layer.closeAll();
                },
                cancel: function(index, layero) {
                    layer.close(index);
                }
            });
        }
        
    //ajax
    qb.ajax = function(ajaxUrl, ajaxMethod, ajaxData, ajaxDataType, ajaxSuccess) {
            showLoad();
            $.ajax({
                url: ajaxUrl,
                type: ajaxMethod || "get",
                data: ajaxData,
                dataType: ajaxDataType || "json",
                //          tradtional:true,
                success: ajaxSuccess,
                complete: function(r) {
                    returnLogin(r.responseJSON);
                    hideLoad();
                }
            });
        }
        //post
    qb.post = function(ajaxUrl, ajaxData, ajaxDataType, ajaxSuccess) {
            var ajaxDataType = ajaxDataType || 'json';
            qb.ajax(ajaxUrl, "Post", ajaxData, ajaxDataType, ajaxSuccess);
        }
        //get
    qb.get = function(ajaxUrl, ajaxData, ajaxDataType, ajaxSuccess) {
            var ajaxDataType = ajaxDataType || 'json';
            qb.ajax(ajaxUrl, "get", ajaxData, ajaxDataType, ajaxSuccess);
        }
        //判断字符长度
    qb.strLen = function(str) {
            var len = 0;
            for (var i = 0; i < str.length; i++) {
                var c = str.charCodeAt(i);
                //单字节加1 
                if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                    len++;
                } else {
                    len += 2;
                }
            }
            return len;
        }
        //判断是否含特殊字符
    qb.specialStr = function(str) {
        var containSpecial = RegExp(/[(\《)(\》)(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/);
        return (containSpecial.test(str));
    }
    
    //加    
    qb.floatPlus = function(arg1,arg2){
    	var r1,r2,m;    
	    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}    
	    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}    
	    m=Math.pow(10,Math.max(r1,r2));    
	    return (arg1*m+arg2*m)/m;    ;
    }
	      
	//减    
	qb.floatSub = function(arg1,arg2){
    	var r1,r2,m,n;    
	    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}    
	    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}    
	    m=Math.pow(10,Math.max(r1,r2));    
	    //动态控制精度长度    
	    n=(r1>=r2)?r1:r2;    
	    return ((arg1*m-arg2*m)/m).toFixed(n);    
    }
	       
	//乘    
	qb.floatMul = function(arg1,arg2){
    	var m=0,s1=arg1.toString(),s2=arg2.toString();     
	    try{m+=s1.split(".")[1].length}catch(e){}     
	    try{m+=s2.split(".")[1].length}catch(e){}     
	    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);  
    }
	      
	//除   
	qb.floatDiv = function(arg1,arg2){
    	var t1=0,t2=0,r1,r2;     
	    try{t1=arg1.toString().split(".")[1].length}catch(e){}     
	    try{t2=arg2.toString().split(".")[1].length}catch(e){}     
	        
	    r1=Number(arg1.toString().replace(".",""));  
	   
	    r2=Number(arg2.toString().replace(".",""));     
	    return (r1/r2)*Math.pow(10,t2-t1);
    }
	
	//浮点数保留位数设置
    qb.floatTofixed = function(float){
    	return float.toFixed(2);
    }
    
    return qb;
}(window);

//手机号正则
function checkPhone(phone){
	var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if (!myreg.test(phone)) {
		return false;
	} else {
		return true;
	}
}
//邮箱正则
function checkEmail(Email){
	var myreg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
	if (!myreg.test(Email)) {
		return false;
	} else {
		return true;
	}
}
//密码正则
function checkPassword(password){
	//字母和数字组合的8-20位密码
	var myreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
	if (!myreg.test(password)) {
		return false;
	} else {
		return true;
	}
}

//二级密码正则
function checkSecPassword(password){
	//6位数字密码
	var myreg = /^\d{6}$/;
	if (!myreg.test(password)) {
		return false;
	} else {
		return true;
	}
}

//过滤手机号
function starPhone(phoneNum){
	var str = String(phoneNum),
	strLen = str.slice(-8, -4);
	return str.replace(strLen, "****");
}

//*****
//*使用时
//** 需要加载jquery.cookie.js，Translate.js，<script src="/content/language.js"></script>
//* 然后调用
//* 页面切换参考language.html
//*//
////*****//


//翻译
function languageJs(){
    var language;
    var language = $.cookie("SelfLanguage");
    if (translateData && language=='英语') {
        setTranslateData(translateData);
        translateDOM(document.body);
        translateDOM("title");
    }
    if ($.cookie("SelfLanguage") == '英语') {
        language = '英语';
    } else {
        language = '中文';
        $.cookie("SelfLanguage",'中文',{path: '/' });
    }
}

//重置字体大小 rem 
//用法
// setRem();
// window.onresize = function () {   //浏览器尺寸变化进行触发window.onresize函数，然后触发函数setRem()
//	 setRem()
//}

function setRem () {
let htmlWidth = document.documentElement.clientWidth ||document.body.clientWidth;  //检测html的屏幕宽度和body的屏幕宽度
document.documentElement.style.fontSize= htmlWidth/7.5 + 'px'; //设置font-size在750屏幕下的尺寸为100px,这样转换的rem可以一目了然之前是多少px的。开发屏幕尺寸自己选，3.2的320屏幕宽也可以。
  }


$(function(){
	//把自定义函数设置为Vue的全局函数
	Vue.prototype.floatTofixed = common.floatTofixed;//浮点数保留位数设置
	Vue.prototype.floatPlus = common.floatPlus;//加
	Vue.prototype.floatSub = common.floatSub;//减
	Vue.prototype.floatMul = common.floatMul;//乘
	Vue.prototype.floatDiv = common.floatDiv;//除
	
	setRem();
   window.onresize = function () {   //浏览器尺寸变化进行触发window.onresize函数，然后触发函数setRem()
		 setRem()
	}
})


(function setPageTopHeader() {
    const div = `<div class="page-top-header"></div>`
    $('body').prepend(div)
})